package org.caro.gui;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import javax.swing.JButton;

import org.caro.actions.ContestantAction;
import org.caro.utils.Constants;

public class UtilityZonePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Create the panel.
	 */
	private JLabel moveLabel;
	private JLabel player1name;
	private JLabel player1score;
	private JLabel player2name;
	private JLabel player2score;
	private JLabel player1turn;
	private JLabel player2turn;
	private JButton newGameButton;
	private JButton menuButton;
	private JButton replayButton;
	private JButton undoButton;
	private JProgressBar progressBar;
	private ContestantAction contestant;
	private ImageIcon icon ;
	private int mode ;
	public UtilityZonePanel(int mode ,ContestantAction contestant) {
		this.mode = mode;
		this.contestant = contestant;
		this.icon = new ImageIcon(Constants.TURN);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 118, 90, 98, 210, 181, 0 };
		gridBagLayout.rowHeights = new int[] { 45, 41, 38, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JLabel lblNewLabel = new JLabel("Tỷ số");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 0;
		add(lblNewLabel, gbc_lblNewLabel);

		JLabel lblTurn = new JLabel("Lượt đánh");
		GridBagConstraints gbc_lblTurn = new GridBagConstraints();
		gbc_lblTurn.insets = new Insets(0, 0, 5, 5);
		gbc_lblTurn.gridx = 2;
		gbc_lblTurn.gridy = 0;
		add(lblTurn, gbc_lblTurn);

		moveLabel = new JLabel("");
		GridBagConstraints gbc_moveLabel = new GridBagConstraints();
		gbc_moveLabel.insets = new Insets(0, 0, 5, 5);
		gbc_moveLabel.gridx = 3;
		gbc_moveLabel.gridy = 0;
		add(moveLabel, gbc_moveLabel);

		newGameButton = new JButton("Tr\u00F2 ch\u01A1i m\u1EDBi");
		GridBagConstraints gbc_newGameButton = new GridBagConstraints();
		gbc_newGameButton.insets = new Insets(0, 0, 5, 0);
		gbc_newGameButton.gridx = 4;
		gbc_newGameButton.gridy = 0;
		add(newGameButton, gbc_newGameButton);

		player1name = new JLabel("Player1");
		GridBagConstraints gbc_player1name = new GridBagConstraints();
		gbc_player1name.insets = new Insets(0, 0, 5, 5);
		gbc_player1name.gridx = 0;
		gbc_player1name.gridy = 1;
		add(player1name, gbc_player1name);

		player1score = new JLabel("0");
		GridBagConstraints gbc_player1score = new GridBagConstraints();
		gbc_player1score.insets = new Insets(0, 0, 5, 5);
		gbc_player1score.gridx = 1;
		gbc_player1score.gridy = 1;
		add(player1score, gbc_player1score);

		player1turn = new JLabel("");
		GridBagConstraints gbc_player1turn = new GridBagConstraints();
		gbc_player1turn.insets = new Insets(0, 0, 5, 5);
		gbc_player1turn.gridx = 2;
		gbc_player1turn.gridy = 1;
		add(player1turn, gbc_player1turn);

		progressBar = new JProgressBar();
		GridBagConstraints gbc_progressBar = new GridBagConstraints();
		gbc_progressBar.insets = new Insets(0, 0, 5, 5);
		gbc_progressBar.gridx = 3;
		gbc_progressBar.gridy = 1;
		add(progressBar, gbc_progressBar);

		replayButton = new JButton("    Ch\u01A1i l\u1EA1i    ");
		GridBagConstraints gbc_replayButton = new GridBagConstraints();
		gbc_replayButton.insets = new Insets(0, 0, 5, 0);
		gbc_replayButton.gridx = 4;
		gbc_replayButton.gridy = 1;
		add(replayButton, gbc_replayButton);

		player2name = new JLabel("");
		GridBagConstraints gbc_player2name = new GridBagConstraints();
		gbc_player2name.insets = new Insets(0, 0, 0, 5);
		gbc_player2name.gridx = 0;
		gbc_player2name.gridy = 2;
		add(player2name, gbc_player2name);

		player2score = new JLabel("0");
		GridBagConstraints gbc_player2score = new GridBagConstraints();
		gbc_player2score.insets = new Insets(0, 0, 0, 5);
		gbc_player2score.gridx = 1;
		gbc_player2score.gridy = 2;
		add(player2score, gbc_player2score);

		player2turn = new JLabel("");
		GridBagConstraints gbc_player2turn = new GridBagConstraints();
		gbc_player2turn.insets = new Insets(0, 0, 0, 5);
		gbc_player2turn.gridx = 2;
		gbc_player2turn.gridy = 2;
		add(player2turn, gbc_player2turn);

		undoButton = new JButton("    Undo    ");
		GridBagConstraints gbc_undoButton = new GridBagConstraints();
		gbc_undoButton.insets = new Insets(0, 0, 0, 5);
		gbc_undoButton.gridx = 3;
		gbc_undoButton.gridy = 2;
		add(undoButton, gbc_undoButton);

		menuButton = new JButton("     Menu     ");
		GridBagConstraints gbc_menuButton = new GridBagConstraints();
		gbc_menuButton.gridx = 4;
		gbc_menuButton.gridy = 2;
		add(menuButton, gbc_menuButton);
		setPanelByMode();
	}


	public void setTurnIconForPlayer1(){
		player1turn.setIcon(this.icon);
		player2turn.setIcon(null);
	}
	public void setTurnIconForPlayer2(){
		player2turn.setIcon(this.icon);
		player1turn.setIcon(null);
	}
	public void addPointForPLayer1(){
		int currentScore = Integer.parseInt(player1score.getText());
		player1score.setText(String.valueOf(++currentScore));
	}
	public void addPointForPlayer2(){
		int currentScore = Integer.parseInt(player2score.getText());
		player2score.setText(String.valueOf(++currentScore));
	}
	private void setPanelByMode() {
		if (this.mode == Constants.AI_MODE) {
			player1name.setText("PLAYER");
			player2name.setText("COMPUTER");
			player1turn.setIcon(this.icon);
		} else if (this.mode == Constants.LAN_MODE) {
			player1name.setText(contestant.getHome());
			player2name.setText(contestant.getAway());
			if (ContestantAction.turn) player1turn.setIcon(this.icon);
			else player2turn.setIcon(icon);
		}
	}
}
